package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Admin;
import com.cskaoyan.mall.bean.Log;
import com.cskaoyan.mall.bean.Role;
import com.cskaoyan.mall.bean.Storage;
import com.cskaoyan.mall.bean.bo.AdminLoginBo;
import com.cskaoyan.mall.bean.vo.AdminOptionVO;
import com.cskaoyan.mall.bean.vo.ObjectsVO;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.mapper.AdminMapper;
import com.cskaoyan.mall.mapper.LogMapper;
import com.cskaoyan.mall.mapper.RoleMapper;
import com.cskaoyan.mall.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Zsp
 * 2021/1/9
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    StorageMapper storageMapper;
    @Autowired
    LogMapper logMapper;


    @Override

    public boolean checkUserLoginInfo(AdminLoginBo loginBo) {
        return adminMapper.selectByUsernameAndPassword(loginBo) == 1 ? true : false;
    }

    @Override
    public ObjectsVO<Admin> getAdminList(Integer page, Integer limit, String sort, String order, String username) {
        PageHelper.startPage(page, limit);
        List<Admin> admins = adminMapper.selectALLOrderByAddTimeDesc(username == null
                ? username : "%" + username + "%", sort, order);
        PageInfo<Admin> adminPageInfo = new PageInfo<>(admins);
        return new ObjectsVO((int) adminPageInfo.getTotal(), admins);
    }

    @Override
    public List<AdminOptionVO> getOptionList() {
        List<AdminOptionVO> adminOptionVOS = new ArrayList<>();
        for (Role role : roleMapper.selectAll()) {
            adminOptionVOS.add(new AdminOptionVO(role));
        }
        return adminOptionVOS;
    }

    @Override
    public ResultVO storageAdminAndInfo(Admin admin, Log log) {
        Admin adminInsert = new Admin(null, admin.getUsername(), admin.getPassword(), null, null,
                admin.getAvatar(), new Date(), new Date(), false, admin.getRoleIds());
        if (adminMapper.selectByUsername(admin) < 1 && adminMapper.insertSelective(adminInsert) > 0) {
            log.setStatus(true);
            log.setResult("创建管理员成功");
            logMapper.insertSelective(log);
            return ResultVO.ok(adminInsert);
        }
        log.setStatus(false);
        log.setResult("创建失败,管理员已存在");
        logMapper.insertSelective(log);
        return ResultVO.error(602, "创建失败,管理员已存在");
    }

    @Override
    @Transactional
    public ResultVO update(Admin admin, Log log) {
        admin.setUpdateTime(new Date());
        if (adminMapper.selectByUsername(admin) > 0 || adminMapper.updateByPrimaryKeySelective(admin) < 1) {
            log.setStatus(false);
            log.setResult("更新失败，管理员已存在");
            logMapper.insertSelective(log);
            return ResultVO.error(10000, "更新失败，管理员已存在");
        }
        log.setStatus(true);
        log.setResult("更新管理员成功");
        logMapper.insertSelective(log);
        return ResultVO.ok(admin);
    }

    @Override
    public ResultVO delete(Admin admin, Log log) {
        if (adminMapper.deleteById(admin) > 0) {
            log.setStatus(true);
            log.setResult("删除管理员成功");
            logMapper.insertSelective(log);
            return ResultVO.ok();
        }
        log.setStatus(false);
        log.setResult("删除失败，管理员已存在");
        logMapper.insertSelective(log);
        return ResultVO.error(10000, "删除失败,管理员已不存在");
    }

    @Override
    public ObjectsVO<Log> getLogList(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        List<Log> logs = logMapper.selectOrderByAddTimeDesc(name == null
                ? name : "%" + name + "%", sort, order);
        PageInfo<Log> logPageInfo = new PageInfo<>(logs);
        return new ObjectsVO((int) logPageInfo.getTotal(), logs);
    }

    @Override
    public ObjectsVO<Storage> getStorageList(Integer page, Integer limit, String sort, String order, String name, String key) {
        PageHelper.startPage(page, limit);
        List<Storage> storages = storageMapper.selectOrderByAddTimeDesc(name == null
                ? name : "%" + name + "%", key, sort, order);
        PageInfo<Storage> storagePageInfo = new PageInfo<>(storages);
        return new ObjectsVO((int) storagePageInfo.getTotal(), storages);
    }

    @Override
    public Integer insertLog(Log log) {
        return logMapper.insertSelective(log);
    }

    @Override
    public ResultVO updateStorage(Storage storage, Log log) {
        storage.setUpdateTime(new Date());
        if (storageMapper.updateByPrimaryKeySelective(storage) < 1) {
            log.setStatus(false);
            log.setResult("更新失败");
            logMapper.insertSelective(log);
            return ResultVO.error(10000, "更新失败");
        }
        log.setStatus(true);
        log.setResult("更新成功");
        logMapper.insertSelective(log);
        return ResultVO.ok(storage);
    }

    @Override
    public ResultVO deleteStorage(Storage storage, Log log) {
        if (storageMapper.deleteById(storage) > 0) {
            log.setStatus(true);
            log.setResult("删除成功");
            logMapper.insertSelective(log);
            return ResultVO.ok();
        }
        log.setStatus(false);
        log.setResult("删除失败");
        logMapper.insertSelective(log);
        return ResultVO.error(10000, "删除失败");
    }

    @Override
    public ObjectsVO<List<Role>> getRoleList(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        List<Role> roles = roleMapper.selectOrderByAddTimeDesc(name == null
                ? name : "%" + name + "%", sort, order);
        PageInfo<Role> rolesPageInfo = new PageInfo<>(roles);
        return new ObjectsVO((int) rolesPageInfo.getTotal(), roles);
    }

    @Override
    public Storage createStorage(Storage storage) {
        int createStorage = storageMapper.insertSelective(storage);
        if (createStorage > 0) {
            return storage;
        }
        return null;
    }

}
